Index: VCard.as
===================================================================
--- VCard.as	(revision 11560)
+++ VCard.as	(working copy)
@@ -7,6 +7,7 @@
  *     Nick Velloff <nick.velloff@gmail.com>
  *     Sean Treadway <seant@oncotype.dk>
  *     Sean Voisen <sean@voisen.org>
+ *     William Herold (Typidee Inc) <william@typidee.com>
  *
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
@@ -23,14 +24,13 @@
  */
 package org.igniterealtime.xiff.vcard
 {
+	import com.hurlant.util.Base64;
+	
 	import flash.display.*;
 	import flash.events.*;
 	import flash.utils.ByteArray;
 	import flash.utils.Timer;
-
-	//import mx.utils.Base64Decoder;
-	import com.hurlant.util.Base64;
-
+	
 	import org.igniterealtime.xiff.core.UnescapedJID;
 	import org.igniterealtime.xiff.core.XMPPConnection;
 	import org.igniterealtime.xiff.data.IQ;
@@ -309,6 +309,12 @@
 		 * @default
 		 */
 		private var _avatar:DisplayObject;
+		
+		/**
+		 *
+		 * @hint for saving avatar
+		 */
+		private var _avatarType:String;
 
 		/**
 		 *
@@ -363,6 +369,7 @@
 			var vcard:VCard = new VCard();
 			vcard.contact = user;
 			cache[ jidString ] = vcard;
+			vcard.jid = user.jid;
 
 			pushRequest( con, vcard );
 
@@ -438,6 +445,22 @@
 		}
 
 		/**
+		 * Set the byte array to be used for an avatar.
+		 */
+		public function set avatar(imageBytes:ByteArray) : void
+		{
+			_imageBytes = imageBytes;
+		}
+		
+		/**
+		 * Set the image type of the avatar. Used for saving the image. If this is blank, the avatar will not be saved.
+		 */
+		public function set avatarType(typeDescription:String) : void
+		{
+			_avatarType = typeDescription;
+		}
+		
+		/**
 		 * Deserializes the incoming IQ to fill the values of this vcard.
 		 * @param iq
 		 */
@@ -608,7 +631,14 @@
 
 					case "AGE":
 						break;
-
+					
+					//there is some ambiguity surrounding how vCard versions are handled
+					//so we need to check it here as well as looking for the attribute
+					//as above.  SEE:  http://xmpp.org/extensions/xep-0054.html#impl
+					case "VERSION":  
+						version = Number(child.text());
+						break;
+						
 					default:
 						trace( "handleVCard. unhandled case child.name(): " + child.name() );
 						break;
@@ -867,7 +897,45 @@
 				homeVoiceNode.NUMBER = homeVoiceNumber;
 				vcardExtNode.appendChild( homeVoiceNode );
 			}
+			
+			if ( avatar != null && _avatarType != null)
+			{
+				//var avatarNode:XMLNode = new XMLNode( 1, 'PHOTO' );
+				var avatarNode:XML = <PHOTO/>
+				var avatarBase64:String;
 
+				try
+				{
+					avatarBase64 = Base64.encodeByteArray(avatar);
+					//encoder.encodeBytes(avatar);
+				}
+				catch(err:Error)
+				{
+					throw new Error("VCard:saveVCard Error encoding bytes " + err.getStackTrace());
+				}
+				
+				try
+				{
+//					var binaryNode:XMLNode = new XMLNode(1, 'BINVAL' );
+//					binaryNode.appendChild( new XMLNode( 3, encoder.toString() ));
+					var binaryNode:XML = <BINVAL/>;
+					binaryNode.appendChild(avatarBase64);
+					avatarNode.appendChild(binaryNode);
+				}
+				catch(err:Error)
+				{
+					throw new Error("VCard:saveVCard Error converting bytes to string " + err.message);
+				}
+
+//				var typeNode:XMLNode = new XMLNode(1, 'TYPE' );
+//				typeNode.appendChild( new XMLNode( 3, _avatarType ));
+				var typeNode:XML = <TYPE/>;
+				typeNode.appendChild(_avatarType);
+				avatarNode.appendChild(typeNode);
+					
+				vcardExtNode.appendChild( avatarNode );	
+			}
+
 			iq.addExtension( vcardExt );
 			con.send( iq );
 		}
